// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Splats Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.flipDir
private alias object.value1 : object.startPos.x
private alias object.value2 : object.startPos.y

private alias object.value17 : debugMode.currentSelection

// Player aliases
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom

// Path ID Aliases
private alias 0 : PATH_A


// ========================
// Function Declarations
// ========================

reserve function Splats_DebugDraw
reserve function Splats_DebugSpawn

// ========================
// Static Values
// ========================

private value Splats_startDebugID = 0


// ========================
// Function Definitions
// ========================

private function Splats_DebugDraw
	temp0 = debugMode[0].currentSelection
	temp0 -= Splats_startDebugID
	DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
end function


private function Splats_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= Splats_startDebugID
	temp0 <<= 1

	CreateTempObject(TypeName[Splats], temp0, object.xpos, object.ypos)

	object[tempObjectPos].direction = object.direction
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos

	if object[tempObjectPos].direction == FLIP_NONE
		object[tempObjectPos].xvel = -0x10000
	else
		object[tempObjectPos].xvel = 0x10000
	end if

end function


// ========================
// Events
// ========================

event ObjectUpdate
	object.priority = PRIORITY_ACTIVE
	object.xpos += object.xvel
	object.ypos += object.yvel
	object.yvel += 0x3800

	if object.yvel > 0
		ObjectTileCollision(CSIDE_FLOOR, 0, 16, PATH_A)
		temp0 = checkResult
		ObjectTileCollision(CSIDE_FLOOR, 0, 17, PATH_A)
		temp0 |= checkResult
		ObjectTileCollision(CSIDE_FLOOR, 0, 18, PATH_A)
		temp0 |= checkResult
		ObjectTileCollision(CSIDE_FLOOR, 0, 19, PATH_A)
		temp0 |= checkResult
		ObjectTileCollision(CSIDE_FLOOR, 0, 20, PATH_A)
		temp0 |= checkResult
		if temp0 == true
			GetBit(temp0, object.propertyValue, 1)
			if temp0 == true
				if object.flipDir == true
					FlipSign(object.xvel)
					object.direction ^= FLIP_X
				end if
				
				object.flipDir ^= true
			end if

			object.yvel = -0x40000
		end if
	end if

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y

		if object.outOfBounds == true
			GetBit(temp0, object.propertyValue, 0)
			if temp0 == FLIP_NONE
				object.direction = FLIP_NONE
				object.xvel = -0x10000
			else
				object.direction = FLIP_X
				object.xvel = 0x10000
			end if
			object.priority = PRIORITY_BOUNDS
			object.state = 0
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -10, -20, 6, 20, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next
end event


event ObjectDraw
	if object.yvel < 0
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	else
		DrawSpriteFX(1, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-11, -15, 21, 40, 214, 211)
		SpriteFrame(-11, -19, 21, 40, 192, 211)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-11, -15, 21, 40, 214, 211)
		SpriteFrame(-11, -19, 21, 40, 192, 211)
	end if

	foreach (TypeName[Splats], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos

		GetBit(temp0, object[arrayPos0].propertyValue, 0)
		if temp0 == false
			object[arrayPos0].direction = FACING_RIGHT
			object[arrayPos0].xvel = -0x10000
		else
			object[arrayPos0].direction = FACING_LEFT
			object[arrayPos0].xvel = 0x10000
		end if
	next

	temp0 = 0
	Splats_startDebugID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[Splats], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Splats_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Splats_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			checkResult &= 3
			break
			
		case 0 // direction
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		case 1 // behaviour
			GetBit(checkResult, object.propertyValue, 1)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		case 1 // behaviour
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 1, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)

	AddEditorVariable("behaviour")
	SetActiveVariable("behaviour")
	AddEnumVariable("Never Change Directions", 0)
	AddEnumVariable("Change Direction On Each Bounce", 1)
end event
